package com.tgy.leetcode;

/**
 * @Author: tgy
 * @Date: 12/22/20 11:27 AM
 *
 *  https://leetcode-cn.com/problems/powx-n/
 */
public class _50_PowMod {

    public static double myPow(int x, int n,int mod) {

        int result = 1;

        int multi = x;

        while (n > 0) {

            if ((n & 1) == 1) {

                result= ((result % mod) * (multi %mod))%mod;
            }

            multi = ((multi%mod)*(multi%mod))%mod;
            n>>=1;
        }

        return result;
    }

    public static void main(String[] args) {

        double resutl = myPow(2,20,5);

        System.out.println(resutl);

    }
}
